#include "TestScene.h"

#include "plan.h"
#include "heightMap.h"
#include "cubeMapObject.h"
#include "FBO.h"

//Managers
#include "shaderManager.h"
#include "shader.h"
#include "textureManager.h"
#include "texture2D.h"
#include "meshManager.h"
#include "objMesh.h"

#include "mathTool.h"

TestScene::TestScene(const std::string& sName, AbstractCamera* pCam):AbstractScene(sName,pCam)
{

}

TestScene::~TestScene()
{
	delete m_fLightPosition;
	delete m_fWhiteColor;
	delete m_fBlackColor;
	delete m_fRedColor;
	delete m_fOrangeColor;

}

bool TestScene::init()
{
	srand ( time(NULL) );

	//Set the value of the light



	m_fLightPosition = new GLfloat[4];
	m_fLightPosition[0] = 0.0;
	m_fLightPosition[1] = 0.0;
	m_fLightPosition[2] = 0.0;
	m_fLightPosition[3] = 1.0;

	//Set the value of the Color
	m_fWhiteColor = new GLfloat[4];
	m_fWhiteColor[0] = 1.0;
	m_fWhiteColor[1] = 1.0;
	m_fWhiteColor[2] = 1.0;
	m_fWhiteColor[3] = 1.0;

	m_fBlackColor = new GLfloat[4];
	m_fBlackColor[0] = 0.0;
	m_fBlackColor[1] = 0.0;
	m_fBlackColor[2] = 0.0;
	m_fBlackColor[3] = 1.0;

	m_fRedColor = new GLfloat[4];
	m_fRedColor[0] = 1.0;
	m_fRedColor[1] = 0.0;
	m_fRedColor[2] = 0.0;
	m_fRedColor[3] = 1.0;

	m_fOrangeColor = new GLfloat[4];
	m_fOrangeColor[0] = 1.0;
	m_fOrangeColor[1] = 0.5;
	m_fOrangeColor[2] = 0.0;
	m_fOrangeColor[3] = 1.0;


	m_fElectricGreenColor = new GLfloat[4];
	m_fElectricGreenColor[0] = 0.6;
	m_fElectricGreenColor[1] = 1.0;
	m_fElectricGreenColor[2] = 0.0;
	m_fElectricGreenColor[3] = 1.0;

	m_fPurpleColor = new GLfloat[4];
	m_fPurpleColor[0] = 0.5;
	m_fPurpleColor[1] = 0.1;
	m_fPurpleColor[2] = 0.7;
	m_fPurpleColor[3] = 1.0;


	glLightfv(GL_LIGHT0, GL_SPECULAR, m_fWhiteColor);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, m_fWhiteColor);
	glLightfv(GL_LIGHT0, GL_AMBIENT, m_fWhiteColor);
	glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.0f);
	glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.01f);
	glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);

	fAngle = 0.0;

	depBallX = 0.0;
	depBallY = 0.0;
	depBallZ = 0.0;
	
	depBallSignX = 1;
	depBallSignY = -1;
	depBallSignZ = 1;

	depBallAttX = 1;
	depBallAttY = 1;
	depBallAttZ = 1;


	depBallSpeedX = 0.01 + (rand()%10)/1000.0;
	depBallSpeedY = 0.01 + (rand()%10)/1000.0;
	depBallSpeedZ = 0.01 + (rand()%10)/1000.0;

	return true;
}

void TestScene::preDisplay()
{
}

void TestScene::display()
{

	//LightParameter


	glMaterialfv(GL_FRONT, GL_SPECULAR,m_fBlackColor);
	glMaterialfv(GL_FRONT, GL_DIFFUSE,m_fWhiteColor);
	glMaterialfv(GL_FRONT, GL_AMBIENT,m_fBlackColor);
	glMaterialf( GL_FRONT, GL_SHININESS, 125.0f);

	glDisable(GL_BLEND);

	glPushMatrix();
		glRotatef(fAngle,0.0,1.0,0.0);
		glTranslatef(6.5,4.0,0.0);
		glLightfv(GL_LIGHT0, GL_POSITION,m_fLightPosition);
		glScalef(0.02,0.02,0.02);
		glColor3f(1.0,1.0,0.0);
		MeshManager::getInstance().getMesh("ball.obj")->Draw();
	glPopMatrix();

	
	ShaderManager::getInstance().getShader("light")->Activate();
	glPushMatrix();
		glScalef(0.5,0.5,0.5);
		//MeshManager::getInstance().getMesh("testIndoor.obj")->Draw();
		TextureManager::getInstance().getTexture2D("../../data/toonwall.jpg")->activate();
		MeshManager::getInstance().getMesh("testIndoor.obj")->Draw(0);
		TextureManager::getInstance().getTexture2D("../../data/toonfloor.jpg")->activate();
		MeshManager::getInstance().getMesh("testIndoor.obj")->Draw(1);
		TextureManager::getInstance().getTexture2D("../../data/toonceiling.jpg")->activate();
		MeshManager::getInstance().getMesh("testIndoor.obj")->Draw(2);
		TextureManager::getInstance().getTexture2D("../../data/toonceiling.jpg")->desactivate();
	glPopMatrix();
	ShaderManager::getInstance().getShader("light")->Desactivate();


	//-----------------Toon shaded stuffs	
	ShaderManager::getInstance().getShader("Toon")->Activate();

	
	glMaterialfv(GL_FRONT, GL_DIFFUSE,m_fElectricGreenColor);
	glPushMatrix();
		glTranslatef(7.0+depBallX,3.0+depBallY,-7.0+depBallZ);
		glScalef(0.3,0.3,0.3);
		MeshManager::getInstance().getMesh("ball.obj")->Draw();
	glPopMatrix();


	glPushMatrix();
		glTranslatef(3.0,4,4.0);
		glRotatef(-fAngle/2.0,1.0,0.0,0.0);	
		glRotatef(fAngle*1.5,0.0,0.0,1.0);
		glScalef(0.18,0.18,0.18);
		MeshManager::getInstance().getMesh("torusKnot.obj")->Draw();
	glPopMatrix();

	glMaterialfv(GL_FRONT, GL_DIFFUSE,m_fRedColor);
	glPushMatrix();
		glTranslatef(3.0,4,4.0);
		glRotatef(fAngle/3.0,1.0,0.0,0.0);	
		glRotatef(fAngle,0.0,0.0,1.0);
		glScalef(0.4,0.4,0.4);
		MeshManager::getInstance().getMesh("torus.obj")->Draw();
	glPopMatrix();


	glMaterialfv(GL_FRONT, GL_DIFFUSE,m_fPurpleColor);
	glPushMatrix();
		glTranslatef(-7.0,1.2,-11.0);
		glRotatef(90.0,0.0,1.0,0.0);
		glScalef(0.05,0.05,0.05);
		MeshManager::getInstance().getMesh("pouet.obj")->Draw();
	glPopMatrix();

	glMaterialfv(GL_FRONT, GL_DIFFUSE,m_fOrangeColor);
	glPushMatrix();
		glTranslatef(-7.0,2.8,-10.2);
		glRotatef(-33.0,1.0,1.0,0.0);
		glScalef(0.1,0.1,0.1);
		glDisable(GL_CULL_FACE);
		MeshManager::getInstance().getMesh("teapot.obj")->Draw();
		glEnable(GL_CULL_FACE);
	glPopMatrix();

	glMaterialfv(GL_FRONT, GL_DIFFUSE,m_fOrangeColor);
	glPushMatrix();
		glTranslatef(-7.0,3.0,-8.8);
		glRotatef(55.0,1.0,1.0,0.0);
		glScalef(0.08,0.08,0.08);
		glDisable(GL_CULL_FACE);
		MeshManager::getInstance().getMesh("teapot.obj")->Draw();
		glEnable(GL_CULL_FACE);
	glPopMatrix();

	glMaterialfv(GL_FRONT, GL_DIFFUSE,m_fOrangeColor);
	glPushMatrix();
		glTranslatef(-7.0,1.9,-7.8);
		glRotatef(90.0,1.0,1.0,0.0);
		glScalef(0.05,0.05,0.05);
		glDisable(GL_CULL_FACE);
		MeshManager::getInstance().getMesh("teapot.obj")->Draw();
		glEnable(GL_CULL_FACE);
	glPopMatrix();

	ShaderManager::getInstance().getShader("Toon")->Desactivate();
	glEnable(GL_BLEND);
	//-----------------End Toon shaded stuffs

	
}
		
void TestScene::update()
{
	fAngle += 0.5;

	depBallX += depBallAttX * 0.01;
	depBallAttX -= depBallSignX * depBallSpeedX;
	if(depBallAttX < -1.0 || depBallAttX > 1.0){
		depBallSignX *= -1;
	}
	
	depBallY += depBallAttY * 0.01;
	depBallAttY -= depBallSignY * depBallSpeedY;
	if(depBallAttY < -1.0 || depBallAttY > 1.0){
		depBallSignY *= -1;
	}

	depBallZ += depBallAttZ * 0.01;
	depBallAttZ -= depBallSignZ * depBallSpeedZ;
	if(depBallAttZ < -1.0 || depBallAttZ > 1.0){
		depBallSignZ *= -1;
	}
}

bool TestScene::isFinished()
{
	return false;
}